<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme


export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    chartData: {
      type: Object,
      required: true
    },
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        // title: {
        //   text: '虚拟机状态分布',
        //   // subtext: '统计',
        //   left: 'center'
        // },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          left: 10,
          data: ['已关机', '已挂起', '运行中',]
        },
        calculable: true,
        color: ['gray','coral','lightgreen',],
        series: [
          {
            name: '虚拟机数量',
            type: 'pie',
            roseType: 'radius',
            radius: [25, 125],
            center: ['50%', '38%'],
            data: [
              { value: this.chartData.shut_down, name: '已关机' },
              { value: this.chartData.hang_up, name: '已挂起' },
              { value: this.chartData.running, name: '运行中' },
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
